//
//  AddEquipVC.m
//  sensen_ios
//
//  Created by mc on 16/3/9.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "AddEquipVC.h"

#import "DeviceListVC.h"

#import "NewUITextField.h"
#import "DeviceInfo.h"



@interface AddEquipVC ()<UITextFieldDelegate>{
    DeviceInfo * _deviceInfo;
}

@end

@implementation AddEquipVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手动添加设备";
     [self _initView];
    // Do any additional setup after loading the view.
}


- (void)viewDidAppear:(BOOL)animated{
    _deviceInfo = [[DeviceInfo alloc] init];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismiss];
    }
}

- (void)_initView{
    [self _initBackgroundView];
    [self _initHeaderView];
    [self _initUITextFieldView];
    [self _initButtonView];
}


- (void)_initHeaderView{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    header.backgroundColor = rgba(201, 202, 203, 1);
    [self.view addSubview:header];
    UILabel *lable = [[UILabel alloc] init];
    [header addSubview:lable];
    lable.text = @"被添加设备参数";
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header).offset(20);
        make.top.equalTo(header).offset(0);
        make.bottom.equalTo(header).offset(0);
        make.width.equalTo(@200);
    }];
    
}


- (void)_initUITextFieldView{
    NSArray * array = @[@"设备DID", @"设备密码",@"设备名称"];
    CGFloat height = 40;
    CGFloat interval  = 3;
    for (int i = 0; i < 3; i++) {
        NewUITextField * textField = [[NewUITextField alloc] initWithFrame:CGRectMake(0, 0, 0, height)];
        textField.headLabel.text = array[i];
        textField.textField.tag = 100 + i;
        textField.textField.delegate = self;
        if (i == 0) {
            textField.frame = CGRectMake(0, 30, SCREEN_WIDTH, height);
            textField.textField.placeholder = @"设备DID";
        }else if (i == 1){
            UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 30 + height, SCREEN_WIDTH, interval)];
            [self.view addSubview:lable];
            lable.backgroundColor = [UIColor whiteColor];
            textField.frame = CGRectMake(0, 30 + height + interval, SCREEN_WIDTH, height);
            textField.textField.placeholder = @"设备密码";
            textField.textField.secureTextEntry = YES;
        
        }else if (i == 2){
            UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 30 + 2 * height + interval, SCREEN_WIDTH, interval)];
            [self.view addSubview:lable];
            lable.backgroundColor = [UIColor whiteColor];
            textField.frame = CGRectMake(0, 30 + 2 * height +  2 * interval, SCREEN_WIDTH, height);
            textField.textField.placeholder = @"设备名称";
        }
        [self.view addSubview:textField];

    }
}




- (void)_initBackgroundView{
    
    UIImageView * backgroundImage = [[UIImageView alloc] init];
    backgroundImage.image = [UIImage imageNamed:@"手动添加设备_03"];
    [self.view addSubview:backgroundImage];
    [backgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-50);
        make.size.mas_equalTo(CGSizeMake(240, 240));
        
    }];
    self.view.backgroundColor = SS_NAVI_GRAY_COLOR;
    
}



- (void)_initButtonView{
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, 40)];
    [self.view addSubview:footer];
    
    
    // 创建确定按钮
    UIButton  * determineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [footer addSubview:determineBtn];
    [determineBtn setTitle:@"确定" forState:UIControlStateNormal];
    [determineBtn addTarget:self action:@selector(determineAction:) forControlEvents:UIControlEventTouchUpInside];
    [determineBtn setBackgroundColor:rgba(0, 140, 216, 1)];
    determineBtn.layer.borderColor = rgba(0, 140, 216, 1).CGColor;
    determineBtn.layer.borderWidth = 1;
    determineBtn.layer.masksToBounds = YES;
    determineBtn.layer.cornerRadius = 3.0;
    
    
    // 创建取消按钮
    UIButton  * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [footer addSubview:cancelBtn];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTintColor:[UIColor blackColor]];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setBackgroundColor:[UIColor clearColor]];
    cancelBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 3.0;

    [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footer).offset(50);
        make.centerY.equalTo(footer.mas_centerY);
        make.height.equalTo(@30);
        make.height.equalTo(cancelBtn);
        make.right.equalTo(cancelBtn.mas_left).offset(-20);
        make.width.equalTo(cancelBtn);
    }];

    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footer).offset(-50);
        make.centerY.equalTo(footer.mas_centerY);
        make.height.equalTo(@30);

    }];

}

- (void)determineAction:(UIButton *)button{
     [self.view endEditing:YES];
    
    UITextField * textField1 = (UITextField *)[self.view viewWithTag:100];
    UITextField * textField2 = (UITextField *)[self.view viewWithTag:101];
    UITextField * textField3 = (UITextField *)[self.view viewWithTag:102];
    _deviceInfo.did = textField1.text;
    _deviceInfo.password = textField2.text;
    _deviceInfo.name = textField3.text;
    
    if (_deviceInfo.did.length == 0 || _deviceInfo.did == nil) {
        [SVProgressHUD showOnlyStatus:@"设备id为空" withDuration:1];
        return;
    }
    
    if (_deviceInfo.password.length == 0 || _deviceInfo.password == nil) {
        [SVProgressHUD showOnlyStatus:@"设备密码为空" withDuration:1];
        return;
    }
    
    if (_deviceInfo.name.length == 0 || _deviceInfo.name == nil) {
        [SVProgressHUD showOnlyStatus:@"设备名字为空" withDuration:1];
        return;
    }
    
    [self requestAddDevice];
}




- (void)requestAddDevice{
    DeviceListVC *deviceListVC = (DeviceListVC *)[self.navigationController.viewControllers objectAtIndex:0];
    [deviceListVC addDevice:_deviceInfo];
    [SVProgressHUD showSuccessWithStatus:@"添加成功" withDuration:1];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)cancelAction:(UIButton *)button{
     [self.view endEditing:YES];
     [self.navigationController popViewControllerAnimated:YES];
}



#pragma  mark --- textFieldDelegate ---

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    if (textField.tag == 100)
    {
        [textField becomeFirstResponder];
        return NO;
    }
    else if (textField.tag == 101)
    {
        [textField resignFirstResponder];
        return NO;
    }
    
    else if (textField.tag == 102)
    {
        [textField resignFirstResponder];
        return NO;
    }

    
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
   
    if (textField.tag == 100) {
       _deviceInfo.did = textField.text;
    }
    else if (textField.tag == 101){
        _deviceInfo.token = textField.text;
    }
    
    else if (textField.tag == 102) {
       _deviceInfo.name = textField.text;
    }
    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
